﻿Imports ATS.HelperClass
Imports ATS.CommonClass
Imports ATS.EntryValidation


Partial Public Class ucLocations2
    Dim dbLocations As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Private _pageIsValid As Boolean = True
    Dim editFlag As Integer
    Dim al As New ArrayList

    Sub pageIsValid(ByVal sender As String, ByVal isValid As Boolean)
        Dim inArray As Boolean = False
        For i = 0 To al.Count - 1
            If al.Item(i) = sender Then
                If isValid Then
                    al.Remove(Trim(sender))
                End If
                inArray = True
                Exit For
            End If
        Next
        If Not inArray Then
            If Not isValid Then
                al.Add(Trim(sender))
            End If
        End If
        If al.Count > 0 Then
            _pageIsValid = False
        Else
            _pageIsValid = True
        End If
    End Sub

    Private Sub ucLocations_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        editFlag = HelperClass.EditMode.Load
        LoadLocations()
        DisableAllControls()
    End Sub

    Private Sub DisableAllControls()
        HelperClass.DisableTextBox(gridLocationDetails)
        HelperClass.DisablePasswordBox(gridLocationDetails)
        HelperClass.DisableCheckBox(gridLocationDetails)
        HelperClass.DisableComboBox(gridLocationDetails)
    End Sub
    Private Sub EnableAllControls()
        HelperClass.EnableTextBox(gridLocationDetails)
        HelperClass.EnablePasswordBox(gridLocationDetails)
        HelperClass.EnableCheckBox(gridLocationDetails)
        HelperClass.EnableComboBox(gridLocationDetails)
    End Sub
    Private Sub ClearAllControls()
        HelperClass.ClearTextBox(gridLocationDetails)
        HelperClass.ClearPasswordBox(gridLocationDetails)
        HelperClass.ClearCheckBox(gridLocationDetails)
        HelperClass.ClearComboBox(gridLocationDetails)
    End Sub

    Private Sub LoadLocations(Optional ByVal userFilter As String = "")
        Dim locationTable = From locationDetails In dbLocations.Locations _
                         Where locationDetails.LocationName.ToUpper.Contains(userFilter.ToUpper) _
                         Order By locationDetails.LocationName

        listBoxLocations.ItemsSource = locationTable
        listBoxLocations.SelectedIndex = 0
        If locationTable.Count > 0 Then
            LoadFields(locationTable)
            ucActions.buttonEditMain.IsEnabled = True
            ucActions.buttonDeleteMain.IsEnabled = True
        Else
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            ClearAllControls()
            'textboxCode.Text = Nothing
        End If
        If locationTable.Count = 1 Then
            ExpanderText.Text = locationTable.Count & " record found"
        Else
            ExpanderText.Text = locationTable.Count & " records found"
        End If
    End Sub
    Private Sub LoadSelectedlocation(Optional ByVal locationFilter As String = "")
        Dim locationTable = From locationDetails In dbLocations.Locations _
                         Where locationDetails.LocationName.ToUpper.Contains(locationFilter.ToUpper) _
                        Order By locationDetails.LocationName

        If locationTable.Count > 0 Then
            LoadFields(locationTable)
            ucActions.buttonEditMain.IsEnabled = True
            ucActions.buttonDeleteMain.IsEnabled = True
        Else
            ClearAllControls()
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            'textboxCode.Text = Nothing
        End If
    End Sub

    Sub LoadFields(ByVal locationDetails As IQueryable)
        Dim lf = From locDetails In locationDetails
        With lf.First
            textboxCode.Text = .LocationCode
            textboxName.Text = .LocationName
            textboxDescription.Text = .LocationDesc
        End With
        pageIsValid(textboxCode.Name, EntryValidation.ValidateText(textboxCode))
        pageIsValid(textboxName.Name, EntryValidation.ValidateText(textboxName))
    End Sub


    Private Sub listBoxlocations_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listBoxLocations.SelectionChanged
        Dim selectedlocation As String = ""
        If Not listBoxLocations.SelectedItem Is Nothing Then
            selectedlocation = listBoxLocations.SelectedItem.locationName.ToString
        End If
        LoadSelectedlocation(selectedlocation)
    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchButton.Click
        LoadLocations(searchBox.Text)
    End Sub

    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
        LoadLocations(searchBox.Text)
    End Sub

    Private Sub ucActions_AddButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.AddButtonClicked
        gridListbox.IsEnabled = False
        editFlag = EditMode.Add
        ClearAllControls()
        EnableAllControls()
    End Sub

    Private Sub ucActions_CancelButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.CancelButtonClicked
        Dim selectedlocation As String = ""
        If Not listBoxLocations.SelectedValue Is Nothing Then
            selectedlocation = listBoxLocations.SelectedItem.LocationName.ToString
        End If


        editFlag = EditMode.Load
        DisableAllControls()
        LoadSelectedlocation(selectedlocation)
        gridListbox.IsEnabled = True
    End Sub

    Private Sub ucActions_DeleteButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.DeleteButtonClicked
        Try
            Dim warningString As String = "Do you really want to delete this record?" & ControlChars.Lf & ControlChars.Lf & listBoxLocations.SelectedItem.locationname.ToString
            If System.Windows.MessageBox.Show(warningString, "Delete Record", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                Dim selectedlocationCode As String = listBoxLocations.SelectedItem.locationcode.ToString
                Dim locationsTable = From locationDetails In dbLocations.Locations _
                             Where locationDetails.LocationCode.ToUpper = (selectedlocationCode.ToUpper)
                Dim exst As Location = locationsTable.First
                dbLocations.Locations.DeleteOnSubmit(exst)
                dbLocations.SubmitChanges()
                LoadLocations()
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
        End Try
    End Sub

    Private Sub ucActions_EditButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.EditButtonClicked
        editFlag = EditMode.Edit
        gridListbox.IsEnabled = False
        EnableAllControls()
        textboxCode.IsEnabled = False
    End Sub

    Private Sub ucActions_SaveButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.SaveButtonClicked
        Try
            If _pageIsValid Then
                Dim ListBoxSelectedItemIndex As Integer = listBoxLocations.SelectedIndex
                If editFlag = EditMode.Edit Then
                    Dim locationsTable = From locationDetails In dbLocations.Locations _
                                 Where locationDetails.LocationCode = textboxCode.Text.ToUpper

                    With locationsTable.First
                        .LocationDesc = textboxDescription.Text
                        .LocationName = textboxName.Text
                    End With
                    dbLocations.SubmitChanges()
                ElseIf editFlag = EditMode.Add Then
                    Dim saveNewlocation As New Location
                    With saveNewlocation
                        .LocationDesc = textboxDescription.Text
                        .LocationName = textboxName.Text
                        .LocationCode = textboxCode.Text
                    End With
                    dbLocations.Locations.InsertOnSubmit(saveNewlocation)
                    dbLocations.SubmitChanges()
                End If
                editFlag = EditMode.Load
                If searchBox.Text.Length > 0 Then
                    LoadLocations(searchBox.Text)
                Else
                    LoadLocations()
                End If

                listBoxLocations.SelectedIndex = ListBoxSelectedItemIndex
                DisableAllControls()
                gridListbox.IsEnabled = True
                _pageIsValid = True
            Else
                ucActions.buttonSaveMain.IsEnabled = True
                ucActions.buttonEditMain.IsEnabled = False
                ucActions.buttonAddMain.IsEnabled = False
                ucActions.buttonDeleteMain.IsEnabled = False
                ucActions.buttonCancelMain.IsEnabled = True
                Dim warningString As String = "Some mandatory fields are missing."
                System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
            ucActions.buttonSaveMain.IsEnabled = True
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonAddMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            ucActions.buttonCancelMain.IsEnabled = True
        End Try
    End Sub

    Private Sub textboxCode_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxCode.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

        If editFlag = EditMode.Add Then
            Dim selectedlocation As String = textboxCode.Text.ToString
            Dim locationTable = From locationDetails In dbLocations.Locations _
                         Where locationDetails.LocationCode = selectedlocation.ToUpper
            If locationTable.Count > 0 And textboxCode.IsEnabled = True Then
                _pageIsValid = False
                textboxCode.BorderBrush = New SolidColorBrush(Colors.Red)
                textboxCode.ToolTip = "Code already exist."
            End If
        End If
    End Sub

    Private Sub textboxName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxName.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

    End Sub
End Class
